package com.lanyam.travel.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lanyam.travel.base.dao.ProductTicketMapper;
import com.lanyam.travel.base.dao.TicketTypeMapper;
import com.lanyam.travel.base.entity.ProductTicket;
import com.lanyam.travel.base.entity.TicketType;
import com.lanyam.travel.base.service.ITicketTypeService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rosson
 * @since 2020-02-12
 */
@Service
public class TicketTypeServiceImpl implements ITicketTypeService {
	
	@Autowired
	private TicketTypeMapper ticketTypeMapper;
	
	@Autowired
	private ProductTicketMapper productTicketMapper;

	@Override
	public List<TicketType> getAllTicketType() {
		// TODO Auto-generated method stub
		return ticketTypeMapper.selectList(null);
	}

	@Override
	public List<ProductTicket> getTickeTypeByProductId(String productId) {
		// TODO Auto-generated method stub
		List<TicketType> allTicketType = getAllTicketType();
		Map<String,TicketType> ticketTypeMap = new HashMap<String, TicketType>();
		for (TicketType ticketType : allTicketType) {
			ticketTypeMap.put(ticketType.getId(), ticketType);
		}
		QueryWrapper<ProductTicket> wrapper = new QueryWrapper<ProductTicket>();
		wrapper.eq("product_id", productId);
		List<ProductTicket> list = productTicketMapper.selectList(wrapper);
		for (ProductTicket productTicket : list) {
			TicketType type= ticketTypeMap.get(productTicket.getTicketTypeId());
			productTicket.setTicketType(type.getTicketType());
			productTicket.setContent(type.getContent());
		}
		return list;
	}

	@Override
	public void createProductTicket(ProductTicket productTicket) {
		// TODO Auto-generated method stub
		productTicketMapper.insert(productTicket);
	}

	@Override
	public void updateProductTicket(ProductTicket ProductTicket) {
		// TODO Auto-generated method stub
		productTicketMapper.updateById(ProductTicket);
	}

	@Override
	public void deleteProductTicketById(String id) {
		// TODO Auto-generated method stub
		productTicketMapper.deleteById(id);
	}

	@Override
	public void deleteProductTicketByProductId(String productId) {
		// TODO Auto-generated method stub
		QueryWrapper<ProductTicket> wrapper = new QueryWrapper<ProductTicket>();
		wrapper.eq("product_id", productId);
		productTicketMapper.delete(wrapper);
	}

	@Override
	public ProductTicket getProductTicketById(String id) {
		// TODO Auto-generated method stub
		ProductTicket productTicket = productTicketMapper.selectById(id);
		TicketType ticketType = ticketTypeMapper.selectById(productTicket.getTicketTypeId());
		productTicket.setContent(ticketType.getContent());
		productTicket.setTicketType(ticketType.getTicketType());
		return productTicket;
	}


}
